package Plugin_Reprise.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import Plugin_Reprise.editors.trf_editor;
import Prog.Objets.ObjetUniverse;

public class UpdateOracleTable  extends AbstractHandler {
	/**
	 * The constructor.
	 */
	public UpdateOracleTable() {
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {

		try {
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("org.eclipse.ui.views.ProgressView");
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		final trf_editor trf_editorcurrent=(trf_editor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		System.out.println(trf_editorcurrent.getTitle());
		System.out.println(trf_editorcurrent.text1.getText());
		final String FichierUV=trf_editorcurrent.text1.getText();
		final String RestrictionFich=trf_editorcurrent.text2.getText();
		final String RestrictionCheck=trf_editorcurrent.text3.getText();
		
		//trf_editorcurrent.results.setText("Synchronisation UV avec Oracle");
				
		Job job = new Job("Synchronisation UV avec Oracle") {
			@Override
			protected IStatus run(IProgressMonitor monitor) {

				ObjetUniverse objUV = new ObjetUniverse(trf_editorcurrent, trf_editorcurrent.out);
				objUV.setColonnes(trf_editorcurrent.colsUV);
				objUV.SynchronisationUVOracle(FichierUV,RestrictionFich,RestrictionCheck,  monitor);
				return Status.OK_STATUS;
				
				
				// Set total number of work units
				//monitor.beginTask("Calcul des checks sum de " + FichierUV, 100);				
				/*for (int i = 0; i < 20; i++) {
					try {
						Thread.sleep(200);
						monitor.subTask("doing " + i);
						// Report that 10 units are done
						monitor.worked(5);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}*/
			}
		};
		
		job.schedule();
		return null;
	}
}